/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xddai.greword;

import com.healthmarketscience.jackcess.ColumnBuilder;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.Table;
import com.healthmarketscience.jackcess.TableBuilder;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 *
 * @author xddai
 */
public class WordAccessDB {

    public static void main(String[] args) throws IOException, SQLException {
	createTable();
	Database db = Database.open(new File("gre.mdb"));
	Table wordTable = db.getTable("word");
	addData(wordTable);
//	System.out.println(wordTable.display());
    }

    public static void createTable() throws IOException, SQLException {
	Database db = Database.create(new File("gre.mdb"));
	Table wordTable = new TableBuilder("word").addColumn(
		new ColumnBuilder("word").setSQLType(Types.VARCHAR).toColumn()).
		addColumn(new ColumnBuilder("explain").setSQLType(Types.LONGVARCHAR).toColumn()).toTable(db);
	wordTable.addRow("abcdxddai", "this is a word database create by xddai at " + new Date() + " Email:hitdong@qq.com");
	db.close();
    }

    public static void addData(Table wordTable) throws FileNotFoundException, IOException {
	BufferedReader sc = new BufferedReader(new InputStreamReader(new FileInputStream("GRE.txt")));
	Map<String,EnglishWord> wordList = new LinkedHashMap<String, EnglishWord>();
	int max = 500000;
	String demp = null;
	int number = 1;
	int lineNumer=1;
	EnglishWord word = new EnglishWord();
	String explain = null;
	while ((demp =sc.readLine())!=null) {
	    lineNumer++;
//	    System.out.println(lineNumer);
	    if (demp.matches("^[\\w]+[\\s]*$")) {
		word.setWord(demp.trim());
		continue;
	    }
//	    if (demp.startsWith("Function") || demp.startsWith("Meaning")||demp.startsWith("[")) {
//		continue;
//	    }
	    if (demp.matches("^[\\s]*$")) {
		if(word.getWord() == null || word.getWord().trim().equals(""))
		    continue;
		explain = word.getExplain();
		if((explain.length()-explain.indexOf("Meaning"))<10){
		    System.out.println("!!!!!!!!!!!!!!!!!!!!!!"+explain+word.getWord());
		    continue;
		}
		addWord(wordList,word);
		word = new EnglishWord();
		number++;
		if (number > max) {
		    break;
		}
		continue;
	    }
	    word.appendExplain(demp);
	}
	System.out.println(number);
	System.out.println(lineNumer);
	for (String dempWord : wordList.keySet()) {
	    wordTable.addRow(dempWord,wordList.get(dempWord).getExplain());
	}
	System.out.println(wordList.size());
	sc.close();
    }
    public static void addWord(Map<String,EnglishWord> wordList,EnglishWord word){
	EnglishWord dempWord = wordList.get(word.getWord());
	if(dempWord == null){
	    wordList.put(word.getWord(), word);
	}else{
	    if(!dempWord.getExplain().equals(word.getExplain())){
		dempWord.appendExplain(word.getExplain());
	    }
	}
    }
}
